package com.cloudwise.controller;

import com.cloudwise.entity.ReturnBean;
import com.cloudwise.entity.Tester;
import com.cloudwise.entity.TesterVo;
import com.cloudwise.service.TesterService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * @description：测试者业务控制器
 * @modified By：
 * @version: 1.0
 */
@RestController
@RequestMapping("tester")
public class TesterController extends BaseController {
    @Resource
    private TesterService testerService;
    /**
     * @description:带分页的查询
     * @param
     * @return com.cloudwise.entity.ReturnBean
     */
    @RequestMapping("selectAllPage")
    public ReturnBean selectAllPage(Long page  ,Long limit,Tester tester) {
        List<TesterVo> testers = testerService.selectAllTester( page  , limit,tester);
        return super.success(testers,testerService.getCount(tester));
    }
    /**
     * 新增数据
     *
     * @param tester 实体对象
     * @return 新增结果
     */
    @PostMapping("insert")
    public ReturnBean insert( @RequestBody Tester tester) {

        //校验手机号是否合法
        boolean checkPhone = testerService.checkPhone(tester.getPhone());
        if(!checkPhone){
            return super.fail(tester,"该手机号已经测试过，请更换手机号");
        }
        //保存测试者信息
        boolean save = this.testerService.saveTester(tester);
        if (save) {
            return super.success(tester);
        } else {
            return super.fail(tester);
        }
    }
    /**
     * @description:删除方法，支持批量删除
     * @param idList
     * @return com.cloudwise.entity.ReturnBean
     */
    @PostMapping("delete")
    public ReturnBean delete( @RequestParam(value = "idList[]",required = false) List<Long> idList) {

        boolean deleteTesterByIds = this.testerService.deleteTesterByIds(idList);
        if (deleteTesterByIds) {
            return super.success(null);
        } else {
            return super.fail(null);
        }
    }

}
